package com.junjie.checkin.controller;

import com.github.pagehelper.PageHelper;
import com.junjie.checkin.request.activity.ActivityPageRequest;
import com.junjie.checkin.response.*;
import com.junjie.checkin.response.admin.ActivityDetailsResponse;
import com.junjie.checkin.response.admin.ActivityResponse;
import com.junjie.checkin.response.front.ActivityFrontResponse;
import com.junjie.checkin.response.front.UserActivityHistoryResponse;
import com.junjie.checkin.service.ActivityService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/activities")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 获取活动列表 (已完成)
     * @param  activityPageRequest 参数参数
     * @return 统一返回结果,放回活动列表
     */
    @GetMapping("")
    public Result<PageResult<ActivityFrontResponse>> getActivities(ActivityPageRequest activityPageRequest, HttpServletRequest request){
        activityPageRequest.initDefaults();
        String token = request.getHeader("Authorization");
        PageResult<ActivityFrontResponse> pageResult = activityService.getActivities(activityPageRequest,token);
        return Result.getResult(ResultStatus.OK,pageResult);
    }

    /**
     * 获取活动详情(已完成)
     * @param id 活动id
     * @return 统一返回结果,返回活动详情
     */
    @GetMapping("/{id}")
    public Result getActivityById(@PathVariable Integer id,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        ActivityDetailsResponse activityById = activityService.getActivityById(id,token);
        return Result.getResult(ResultStatus.OK,activityById);
    }

    /**
     * 报名活动
     * @param id 活动id
     * @param request 统一返回结果
     * @return 统一返回结果,无返回数据
     */
    @PostMapping("/{id}")
    public Result signUpActivity(@PathVariable Integer id, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        activityService.signUpActivity(id,token);
        return Result.getResult(ResultStatus.OK);
    }


    /**
     * 获取用户已报名的活动
     * @param request HTTP请求,获取用户令牌
     * @param pageRequest 分页请求参数
     * @return 统一返回结果,返回用户已报名的活动列表
     */
    @GetMapping("/participated")
    public Result<PageResult<UserActivityHistoryResponse>> getParticipatedActivities(HttpServletRequest request, ActivityPageRequest pageRequest){
        pageRequest.initDefaults();
        String token = request.getHeader("Authorization");
        PageResult<UserActivityHistoryResponse> pageResult = activityService.getParticipatedActivities(token,pageRequest);
        return Result.getResult(ResultStatus.OK,pageResult);
    }

    /**
     * 签到活动
     * @param id 活动id
     * @param request 统一返回结果
     * @return 统一返回结果,无返回数据
     */
    @PostMapping("/{id}/checkIn")
    public Result checkInActivity(@PathVariable Integer id,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        activityService.checkInActivity(id,token);
        return Result.getResult(ResultStatus.OK);
    }
}